package com.foxmail.yusp.mycloud;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    // ui
    @BindView(R.id.txt_temp)
    TextView txt_temp;
    @BindView(R.id.txt_humi)
    TextView txt_humi;
    @BindView(R.id.txt_mqtt_status)
    TextView txt_mqtt_status;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private MqttHistoryAdapter mAdapter;
    private MyMQTT myMQTT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        // history
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MqttHistoryAdapter(new ArrayList<String>());
        mRecyclerView.setAdapter(mAdapter);

        myMQTT = new MyMQTT(getApplicationContext(), "/th",
                "tcp://m13.cloudmqtt.com:18957", "yusp", "60240abc");
        myMQTT.setStateListener(new MyMQTT.StateListener() {
            @Override
            public void stateListner(String state) {
                addToHistory(state);
            }
        });
        myMQTT.setMsgListener(new MyMQTT.MsgListener() {
            @Override
            public void msgListner(JSONObject obj) {
                try {
                    txt_temp.setText(String.format(Locale.CHINA, "%2.1f℃", obj.getDouble("t")));
                    txt_humi.setText(String.format(Locale.CHINA, "%2.1f%%", obj.getDouble("h")));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        myMQTT.connect();
    }

    private void addToHistory(String mainText) {
        mAdapter.add(mainText);
/*        Snackbar.make(findViewById(android.R.id.content), mainText, Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();*/

    }

    // 菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.menu_device:
                intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_relay:
                intent = new Intent(this, RelayActivity.class);
                startActivity(intent);
                break;
            case R.id.menu_about:
                break;
            case R.id.menu_support:
                break;
        }
        return true;
    }


    //
}
